{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Odd Number in String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #math #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数学 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largestOddNumber"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中的最大奇数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>num</code> ，表示一个大整数。请你在字符串 <code>num</code> 的所有 <strong>非空子字符串</strong> 中找出 <strong>值最大的奇数</strong> ，并以字符串形式返回。如果不存在奇数，则返回一个空字符串<em> </em><code>\"\"</code><em> </em>。</p>\n",
    "\n",
    "<p><strong>子字符串</strong> 是字符串中的一个连续的字符序列。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"52\"\n",
    "<strong>输出：</strong>\"5\"\n",
    "<strong>解释：</strong>非空子字符串仅有 \"5\"、\"2\" 和 \"52\" 。\"5\" 是其中唯一的奇数。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"4206\"\n",
    "<strong>输出：</strong>\"\"\n",
    "<strong>解释：</strong>在 \"4206\" 中不存在奇数。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"35427\"\n",
    "<strong>输出：</strong>\"35427\"\n",
    "<strong>解释：</strong>\"35427\" 本身就是一个奇数。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= num.length <= 10<sup>5</sup></code></li>\n",
    "\t<li><code>num</code> 仅由数字组成且不含前导零</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-odd-number-in-string](https://leetcode.cn/problems/largest-odd-number-in-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-odd-number-in-string](https://leetcode.cn/problems/largest-odd-number-in-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"52\"', '\"4206\"', '\"35427\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        for i in range(n):\n",
    "            tmp = n-i\n",
    "            if int(num[tmp-1]) % 2 == 1:\n",
    "                return num[:tmp]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = list(num)\n",
    "        while s and int(s[-1]) % 2 == 0:\n",
    "            s.pop()\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        i = 0\n",
    "        for number in num[::-1]:\n",
    "            if int(number) % 2 == 1:\n",
    "                break\n",
    "            i += 1\n",
    "            if i == len(num):\n",
    "                return \"\"\n",
    "        s = \"\".join(num[0:len(num)-i])\n",
    "        return s\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                # 找到第一个值为奇数的字符，返回 num[0:i+1]\n",
    "                return num[:i+1]\n",
    "        # 未找到值为奇数的字符，返回空字符串\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        for i in range(n - 1, -1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                # 找到第一个值为奇数的字符，返回 num[0:i+1]\n",
    "                return num[:i+1]\n",
    "        # 未找到值为奇数的字符，返回空字符串\n",
    "        return \"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        reidx = 0\n",
    "        idx = len(num) - 1\n",
    "        while idx >= 0 :\n",
    "            if int(num[idx]) % 2 == 1 :\n",
    "                reidx = idx + 1\n",
    "                break\n",
    "            idx = idx - 1\n",
    "        return num[0:reidx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        this = 0\n",
    "        for i in range(0,len(num)):\n",
    "            number = ord(num[i])-48\n",
    "            if number%2 ==1:\n",
    "                this = i+1\n",
    "        if this ==0 and (ord(num[0])-48)%2 == 1:\n",
    "            return num[0]\n",
    "        return num[0:this]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        l = len(num)\n",
    "        odigi = -1\n",
    "        for digi in range(l-1,-1,-1):\n",
    "            if int(num[digi])%2:\n",
    "                odigi = digi\n",
    "                break\n",
    "        if odigi == -1: return \"\"\n",
    "        else : return num[:odigi+1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num)-1, -1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                return num[:i+1]\n",
    "        return ''\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        l = len(num)\n",
    "        odigi = -1\n",
    "        for digi in range(l):\n",
    "            if int(num[digi])%2:\n",
    "                odigi = digi\n",
    "        if odigi == -1: return \"\"\n",
    "        else : return num[:odigi+1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num) - 1, -1, -1):\n",
    "            if ord(num[i]) & 1:\n",
    "                return num[:i + 1]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        index = -1\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if int(num[i]) & 1 == 1:\n",
    "                index = i\n",
    "                break\n",
    "        if index == -1:\n",
    "            return ''\n",
    "        return num[: index + 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num) - 1, -1, -1):\n",
    "            if int(num[i]) % 2 != 0:\n",
    "                return num[: i + 1]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                return num[:i+1]\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        ans = 0\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                if i > 0:\n",
    "                    return num[:i+1]\n",
    "                else:\n",
    "                    ans = max(int(num[i]), ans)\n",
    "        return str(ans) if ans else \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            if int(num[i])%2!=0:\n",
    "                return num[:i+1]\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        answer = ''\n",
    "        for i in range(len(num)-1, -1, -1):\n",
    "            if int(num[i]) % 2:\n",
    "                answer += num[0:i+1]\n",
    "                break\n",
    "        return answer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num)):\n",
    "            if int(num[len(num)-i-1:len(num)-i]) % 2 > 0: \n",
    "                return num[:len(num)-i]\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            if int(num[i])%2==1:\n",
    "                return num[:i+1]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        i = len(num)\n",
    "        for c in num[::-1]:\n",
    "            if int(c) % 2 != 0:\n",
    "                break\n",
    "            i -= 1\n",
    "        return num[:i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            if int(num[i]) % 2 != 0:\n",
    "                return num[:i+1]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        i = len(num) - 1\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(1000000)\n",
    "        while i >= 0:\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                return num[:i+1]\n",
    "            else:\n",
    "                i-=1\n",
    "        \n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        res = None\n",
    "        sl = list(num)\n",
    "        for i in range(len(sl)-1,-1,-1):\n",
    "            if int(sl[i])%2:\n",
    "                res = ''.join(sl[:i+1])\n",
    "                break\n",
    "        return res if res else \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num_list = list(num)\n",
    "        for i in range(len(num_list)-1, -1, -1):\n",
    "            if int(num_list[i]) % 2 != 0:\n",
    "                return num[0:i+1]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        l = list(num)\n",
    "        i = len(l) - 1\n",
    "        while i >= 0:\n",
    "            if int(l[i]) % 2 == 0:\n",
    "                i -= 1\n",
    "            else:\n",
    "                return ''.join(l[0: i + 1])\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        for i in range(len(num) - 1, - 1, -1):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                return \"\".join(num[:i+1])\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        numList = list(num)\n",
    "        tar = -1\n",
    "        for idx in range(len(numList) - 1,-1,-1):\n",
    "            temp = ord(numList[idx]) - ord('0')\n",
    "            if temp % 2 == 1:\n",
    "                tar = idx\n",
    "                break\n",
    "        print(tar)\n",
    "        if tar == -1:\n",
    "            return ''\n",
    "        return ''.join(numList[:tar + 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num = list(num)\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            if int(num[i])%2 != 0:\n",
    "                return ''.join(num[0:i+1])\n",
    "        else:\n",
    "            return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        nums=list(num)\n",
    "        for i in range(1,len(nums)+1):\n",
    "            if int(nums[-i])%2==1:\n",
    "                return num[:len(nums)+1-i]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from math import *\n",
    "from typing import *\n",
    "from collections import Counter, deque, defaultdict\n",
    "from bisect import *\n",
    "from heapq import heappop, heappush, heapify\n",
    "from functools import lru_cache\n",
    "from itertools import accumulate, product\n",
    "from sortedcontainers import SortedList\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num = num[::-1]\n",
    "\n",
    "        for i, c in enumerate(num):\n",
    "            if int(c) % 2 != 0:\n",
    "                return num[i:][::-1]\n",
    "\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        last_index = 0\n",
    "        num = list(num)\n",
    "        for i,n in enumerate(num):\n",
    "            if n in \"13579\":\n",
    "               last_index = i+1\n",
    "        return \"\".join(num[:last_index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num = list(num)\n",
    "        for i in range(len(num) - 1, -1, -1):\n",
    "            if int(num[i]) % 2 != 0:\n",
    "                return ''.join(num[:i + 1])\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = str(num)\n",
    "        endindex = -1\n",
    "        result = \"\"\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if int(s[i])%2 == 1:\n",
    "                endindex = i\n",
    "                break\n",
    "        if endindex == -1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            for i in range(endindex+1):\n",
    "                result = result + s[i]\n",
    "        return result\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        idx = []\n",
    "        for i, x in enumerate(num):\n",
    "            if int(x)%2:\n",
    "                idx.append(i)\n",
    "        if len(idx)==0:\n",
    "            return \"\"\n",
    "        else:\n",
    "            return num[:idx[-1]+1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = str(num)\n",
    "        endindex = -1\n",
    "        index = 0\n",
    "        result = \"\"\n",
    "        for i in range(len(s)-1,-1,-1):\n",
    "            if int(s[i])%2 == 1:\n",
    "                endindex = i\n",
    "                break\n",
    "        if endindex == -1:\n",
    "            return \"\"\n",
    "        else:\n",
    "            for i in range(endindex+1):\n",
    "                result = result + s[i]\n",
    "        return result\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        rr = []\n",
    "        for i in range(len(num)-1, -1, -1):\n",
    "            if int(num[i: i+1]) % 2 == 1:\n",
    "                rr.append(i+1)\n",
    "\n",
    "        if int(num[-1]) % 2 == 1:\n",
    "            return num \n",
    "        for leng in rr:\n",
    "            nn = [num[i: i+leng] for i in range(len(num) - leng) if int(num[i+leng-1:i+leng])%2 == 1]\n",
    "            nn.sort(reverse=True)\n",
    "            if len(nn)>0:\n",
    "                return nn[0]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\r\n",
    "def solve(num):\r\n",
    "        tmp = []\r\n",
    "        for ind,i in enumerate(num):\r\n",
    "            if int(i)%2 != 0:\r\n",
    "                tmp.append(ind)\r\n",
    "        print(tmp)\r\n",
    "        if len(tmp) == 0:\r\n",
    "            return ''\r\n",
    "        if tmp[-1] == 0:\r\n",
    "            return num[0]\r\n",
    "        return num[:tmp[-1]]\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def largestOddNumber(self, num: str) -> str:\r\n",
    "        tmp = []\r\n",
    "        for ind,i in enumerate(num):\r\n",
    "            if int(i)%2 != 0:\r\n",
    "                tmp.append(ind)\r\n",
    "        if len(tmp) == 0:\r\n",
    "            return ''\r\n",
    "        if tmp[-1] == 0:\r\n",
    "            return num[0]\r\n",
    "        return num[:tmp[-1]+1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        index = -1\n",
    "        for i in range(n):\n",
    "            if int(num[i]) & 1 == 1:\n",
    "                index = i\n",
    "        return ''.join(num[i] for i in range(index + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        if len(set(\"13579\")&set(num))==0:\n",
    "            return \"\"\n",
    "        \n",
    "        odd_num_index = []\n",
    "        for i in range(len(num)):\n",
    "            if num[i] in \"13579\":\n",
    "               odd_num_index.append(i)\n",
    "\n",
    "\n",
    "        return num[:odd_num_index[-1]+1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        l=len(num)\n",
    "        x=list(num)\n",
    "        b=\"\"\n",
    "        for i in range(l):\n",
    "            c=int(x[-1])\n",
    "            if c%2==1:\n",
    "                break\n",
    "            else:\n",
    "                del x[-1]\n",
    "        for o in x:\n",
    "            b+=o\n",
    "\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        stack = []\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) % 2 == 1:\n",
    "                stack.append(i)\n",
    "        if len(stack) != 0:\n",
    "            return num[:(stack[-1] + 1)]\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        index = -1\n",
    "        for i in range(n-1, -1, -1):\n",
    "            if int(num[i]) & 1 == 1:\n",
    "                index = i\n",
    "                break\n",
    "        return ''.join(num[i] for i in range(index + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        result = []\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            char = num[i]\n",
    "            if int(char)&1==1:\n",
    "                result.append(char)\n",
    "            elif len(result)>0:\n",
    "                result.append(char)\n",
    "        result.reverse()\n",
    "        result = ''.join(result)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        li = []\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) % 2 !=0:\n",
    "                li.append(i)\n",
    "        if li == []:\n",
    "            return \"\"\n",
    "        return num[0:li[-1]+1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = list(num[::-1])\n",
    "        for i in range(len(s)):\n",
    "            if int(s[i])%2:\n",
    "                return \"\".join(s[i:][::-1])\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        ls = list(num)[::-1]\n",
    "        lss = []\n",
    "        for i in range(len(ls)):\n",
    "            if int(ls[i])%2 == 1:\n",
    "                lss = ls[i:][::-1]\n",
    "                break\n",
    "        if lss:\n",
    "            return ''.join(lss)\n",
    "        else:\n",
    "            return ''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = list(num[::-1])\n",
    "        for i in range(len(s)):\n",
    "            if int(s[i])%2:\n",
    "                return \"\".join(s[i:][::-1])\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        s = list(num[::-1])\n",
    "        for i in range(len(s)):\n",
    "            if int(s[i])%2:\n",
    "                return \"\".join(s[i:][::-1])\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        digits = list(num)\n",
    "        digits.reverse()\n",
    "\n",
    "        for i in range(len(digits)):\n",
    "            if int(digits[i]) % 2 == 1:\n",
    "                return num[:len(num) - i]\n",
    "\n",
    "        return ''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "\n",
    "        \n",
    "        odd_num_index = []\n",
    "        for i in range(len(num)):\n",
    "            if num[i] in \"13579\":\n",
    "               odd_num_index.append(i)\n",
    "\n",
    "        if odd_num_index==[]:\n",
    "            return \"\"\n",
    "        return num[:odd_num_index[-1]+1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        digits = list(num)\n",
    "        digits.reverse()\n",
    "\n",
    "        for i in range(len(digits)):\n",
    "            if int(digits[i]) % 2 == 1:\n",
    "                return num[:len(num) - i]\n",
    "\n",
    "        return ''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestOddNumber(self, num: str) -> str:\r\n",
    "        str_num = list(str(num))\r\n",
    "        for i in range(len(num)-1,-1,-1):\r\n",
    "            if int(str_num[i]) % 2 == 1:\r\n",
    "                return num[:i+1]\r\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        maxlen = 0 \n",
    "        n = len(num)\n",
    "        dp = [0] * (n + 1)\n",
    "        odd = '13579'\n",
    "        even = '24680'\n",
    "        last = -1\n",
    "        for i in range(n):\n",
    "            if num[i] in odd:\n",
    "                last = i \n",
    "\n",
    "\n",
    "        return num[:last+1] \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestOddNumber(self, num: str) -> str:\r\n",
    "        num_listr = list(num)\r\n",
    "        if (\"1\" not in num_listr):\r\n",
    "            if (\"3\" not in num_listr):\r\n",
    "                if (\"5\" not in num_listr):\r\n",
    "                    if (\"7\" not in num_listr):\r\n",
    "                        if (\"9\" not in num_listr):\r\n",
    "                            return \"\"\r\n",
    "        i = len(num)\r\n",
    "        while num_listr[-1] not in [\"1\", \"3\", \"5\", \"7\", \"9\"]:\r\n",
    "            num_listr.pop()\r\n",
    "            i -= 1\r\n",
    "        return num[:i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        test = {'1','3','5','7','9'}\n",
    "        res = \"\"\n",
    "        for c in range(len(num)-1,-1,-1):\n",
    "            if num[c] in test :\n",
    "                res = res.join(num[0:c+1])\n",
    "                break\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestOddNumber(self, num: str) -> str:\r\n",
    "        num_listr = list(num)\r\n",
    "        justify = [x in num_listr for x in \"13579\"]\r\n",
    "        if not any(justify):\r\n",
    "            return \"\"\r\n",
    "        i = len(num)\r\n",
    "        while num_listr[-1] not in list(\"13579\"):\r\n",
    "            num_listr.pop()\r\n",
    "            i -= 1\r\n",
    "        return num[:i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        test = {'1','3','5','7','9'}\n",
    "        res = \"\"\n",
    "        for c in range(len(num)-1,-1,-1):\n",
    "            if num[c] in test :\n",
    "                res = res.join(num[0:c+1])\n",
    "                break\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        nlist = [s for s in num]\n",
    "        size = len(nlist)\n",
    "\n",
    "        while size != 0:\n",
    "            digit = int(nlist[size - 1])\n",
    "            if digit % 2 != 0:\n",
    "                break\n",
    "            size -= 1\n",
    "        \n",
    "        return num[:size]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num=[c for  c in num]\n",
    "        n=len(num)\n",
    "        idx=-1\n",
    "        for i in range(n-1,-1,-1):\n",
    "            if int(num[i])%2 == 1 :\n",
    "                idx=i\n",
    "                break\n",
    "        #print(idx)\n",
    "        if idx==-1:\n",
    "            return ''\n",
    "        return ''.join(num[:idx+1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def largestOddNumber(self, num: str) -> str:\r\n",
    "        num_listr = [int(k) for k in num]\r\n",
    "\r\n",
    "        i = 0\r\n",
    "        try:\r\n",
    "            while num_listr[-1] not in [1, 3, 5, 7, 9]:\r\n",
    "                num_listr.pop()\r\n",
    "                i -= 1\r\n",
    "            return num[:] if i == 0 else num[:i]\r\n",
    "        except Exception:\r\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num = list(num)\n",
    "        for i in num[::-1]:\n",
    "            i = int(i)\n",
    "            if i % 2 == 0:\n",
    "                num.pop()\n",
    "            else:\n",
    "                break\n",
    "                break\n",
    "        return ''.join(num)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        num = list(num)\n",
    "        for i in range(len(num)-1,-1,-1):\n",
    "            if int(num[i])%2 == 1:\n",
    "                return \"\".join(num[:i+1])\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestOddNumber(self, num: str) -> str:\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        lst=list(map(int,num))\n",
    "        ans=0\n",
    "        for i in range(len(lst)-1,-1,-1):\n",
    "                if lst[i]%2==1:\n",
    "                    for j in range(0,i+1):\n",
    "                        ans=ans*10+lst[j]\n",
    "                    break\n",
    "        if ans==0:\n",
    "            return(\"\")\n",
    "        else:\n",
    "            return(str(ans))\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
